﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using TestAgentDatabases.Properties;

namespace TestAgentDatabases
{
    class Program
    {
        static void Main(string[] args)
        {
            ThreadPool.SetMaxThreads(20, 20);

            int agents = Settings.Default.Agents;
            int agentsPerDatabase = Settings.Default.AgentsPerDatabase;
            int insertsPerAgent = Settings.Default.InsertsPerAgent;
            Console.WriteLine("Agents = {0}, agents per db = {1}, inserts per agent = {2}", agents, agentsPerDatabase, insertsPerAgent);
            
            Console.WriteLine();
            Console.WriteLine("Testing SQL Server Compact Edition");
            RunTest(new SQLServerCompactTest { DatabaseDirectory = Settings.Default.SqlServerCEDirectory }, agents, agentsPerDatabase, insertsPerAgent);

            //Console.WriteLine();
            //Console.WriteLine("Testing SQLite");
            //RunTest(new SQLiteTest { DatabaseDirectory = Settings.Default.SQLiteDirectory }, agents, agentsPerDatabase, insertsPerAgent);

            Console.WriteLine();
            Console.WriteLine("Testing SQL Server Express");
            RunTest(new SQLServerExpressTest{DatabaseDirectory=Settings.Default.SqlServerExpressDirectory}, agents, agentsPerDatabase, insertsPerAgent);

            Console.ReadKey();
        }

        private static void RunTest(DatabaseTest test, int agents, int agentsPerDatabase, int insertsPerAgent)
        {
            test.Agents = agents;
            test.AgentsPerDatabase = agentsPerDatabase;
            test.InsertsPerAgent = insertsPerAgent;  

            test.EnsureDirectoryExists();

            var sw = new Stopwatch();
            test.DeleteDatabases();
            Console.WriteLine("deleted files");

            sw.Start();
            test.CreateDatabases();
            sw.Stop();
            Console.WriteLine("Took {0:N2} secs to create {1} databases", sw.Elapsed.TotalSeconds, test.GetDatabaseCount());

            sw.Reset();
            sw.Start();
            test.InsertData();
            sw.Stop();
            Console.WriteLine("Took {0:N2} secs to insert {1} rows, {2:N0} rows/sec", sw.Elapsed.TotalSeconds, test.Agents * test.InsertsPerAgent, (test.Agents * test.InsertsPerAgent) / sw.Elapsed.TotalSeconds);
        }
    }
}
